/**
  ******************************************************************************
  * @file    IO_Toggle/main.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    19-September-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************  
  */ 

/* Includes ------------------------------------------------------------------*/
#include "main.h"

uint8_t au8_Buffer_Terminal[LENGTH_MAX_COMMAND];
uint8_t au8_Command[LENGTH_MAX_COMMAND];
uint8_t au8_Data[LENGTH_MAX_COMMAND];
volatile uint8_t u8_Flag_Receive_Terminal = 0;

typeTime pTimer;

extern CtrlPeriph CtrlPeriph_Var[];


void delay_main(uint32_t timer_delay)
{
  uint32_t i, j;
  for(i = 0; i < timer_delay; i++)
    for(j = 0; j < timer_delay; j++);
}
    
void test_lcd5110(void)
{
  SetPosition_LCD5110(0,0);
  clean_screen();
  /* test code */
  uint8_t j;
  for (j = 'A'; j < 'Z'; j++)
  {
    PrintFont5x8(j);
  }
  SetPosition_LCD5110(0,2);
  PrintString_LCD5110("Tuan Anh :) +");
  SetPosition_LCD5110(0,3);
  PrintString_LCD5110("Lon yeu :)");
  delay_main(TIMEROUT_MAIN);
  clean_screen();
}

void DisplayTimer_LCD5110(typeTime *inputTime)
{
  /* hour */
  delay_main(DISPLAY_TIMER);
  clean_screen();
  SetPosition_LCD5110(1,0);
  DisplayNumber(inputTime->Hour,2);
  PrintFont5x8(':');
  /* minute */
  DisplayNumber(inputTime->Min,2);
  PrintFont5x8(':');
  /* second */
  DisplayNumber(inputTime->Sec,2);
  
  SetPosition_LCD5110(1,1);
  DisplayNumber(inputTime->Date,2);
  PrintFont5x8('/');
  DisplayNumber(inputTime->Month,2);
  PrintFont5x8('/');
  DisplayNumber(inputTime->Year,2);

}

void DesplayTerminal_LCD5110(uint8_t *pu8_String)
{
	/* clean_screen(); */
	uint8_t i = 0;
	while(i <= LENGTH_MAX_COMMAND)
	{
		SetPosition_LCD5110(i,5);
		WriteData_LCD5110(0);
		i++;
	}
	SetPosition_LCD5110(0,5);
	PrintString_LCD5110(pu8_String);
}

void test_terminal()
{
  Send_String("\nDa ket thoi thang cong USART !\n");
}

void Init_LED(void)
{
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);
}

void test_led()
{
  STM_EVAL_LEDOn(LED4);
  delay_main(DISPLAY_TIMER);
  STM_EVAL_LEDOff(LED4);
  
  STM_EVAL_LEDOn(LED3);
  delay_main(DISPLAY_TIMER);
  STM_EVAL_LEDOff(LED3);
  
  STM_EVAL_LEDOn(LED5);
  delay_main(DISPLAY_TIMER);
  STM_EVAL_LEDOff(LED5);
  
  STM_EVAL_LEDOn(LED6);
  delay_main(DISPLAY_TIMER);
  STM_EVAL_LEDOff(LED6);
}

void Intterrupt_Receive_Terminal(void)
{
	uint8_t u8_Counter_Command;

	if (((uint8_t)1) == u8_Flag_Receive_Terminal)
	{
		u8_Flag_Receive_Terminal = ((uint8_t)0);
		Terminal_Command_Data(au8_Buffer_Terminal, au8_Command, au8_Data);
		u8_Counter_Command = 0;
		while(CtrlPeriph_Var[u8_Counter_Command].pu8_Command != NULL)
		{
			if (strcmp(au8_Command,CtrlPeriph_Var[u8_Counter_Command].pu8_Command) == 0)
			{
				CtrlPeriph_Var[u8_Counter_Command].Exe_Func(au8_Command, au8_Data);
				break;
			}
			u8_Counter_Command++;
		}
	}
}

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{

	Init_LCD5110(3);
	Init_DS130X();
	Init_LED();
	Terminal_Init();
	__enable_irq();

	test_led();
	test_lcd5110();
	test_terminal();

	/*init timer */
	pTimer.Sec = 0;
	pTimer.Min = 0;
	pTimer.Hour = 0;
	pTimer.Date = 1;
	pTimer.Month = 1;
	pTimer.Day = 1;
	pTimer.Year = 0;
	pTimer.PM_AM = 0;
	pTimer.M12_24 = 0;

	/* ReadTime_DS130X(&pTimer); */
	WriteTime_DS130X(&pTimer);


	while(1)
	{
		ReadTime_DS130X(&pTimer);
		DisplayTimer_LCD5110(&pTimer);

		Intterrupt_Receive_Terminal();
		DesplayTerminal_LCD5110(au8_Buffer_Terminal);

		/* Polling_Receive_Terminal(); */

		//    delay_main(DISPLAY_TEST);
		//    clean_screen();
		//    SetPosition_LCD5110(5,3);
		//    DisplayNumber(i++,2);

		/* Polling
          u8_Char = Receive_Char();
          if (u8_Char == 13)
          {
            Phan_Tach(au8_Buffer_Terminal, au8_Command, au8_Data);
            u8_Counter_Command = 0;
            while(CtrlPeriph_Var[u8_Counter_Command].pu8_Command != NULL)
            {
              if (strcmp(au8_Command,CtrlPeriph_Var[u8_Counter_Command].pu8_Command) == 0)
              {
                CtrlPeriph_Var[u8_Counter_Command].Exe_Func(au8_Command, au8_Data);
                break;
              }
              u8_Counter_Command++;
            }
            DesplayTerminal_LCD5110(au8_Command);
            Send_String(au8_Command);
            Send_String("\t");
            Send_String(au8_Data);
            i=0;
          }
          else
          {
            au8_Buffer_Terminal[i] = u8_Char;
            i++;
            au8_Buffer_Terminal[i] = '\0';
          }
          */
	}
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
